#ifndef _MyTemperature_H
#define _MyTemperature_H

#include "Arduino.h"
#include "MAX31865.h"

#include "../MyEEPROM/MySetting.h"

#include "MyFit.h"

/**
 * MAX31865参考电阻应该用400欧不知道为什么买来的模块都是用430欧
 * 而且把设置改成430后还是不准
 * 
 * 于是自行标定0度和100度对应的原始数据 * 
 * 根据PT100给出的温度-电阻对应表，以50度为一个阶梯，直接用原始数据计算温度 
 * 
 * 以下是各温度阶段斜率和0-100度斜率的比值（0-250度）
 * 
**/
// #define RATE_0_50 1.007531
// #define RATE_50_100 0.992469
// #define RATE_100_150 0.977408
// #define RATE_150_200 0.962347
// #define RATE_200_250 0.947286

// #define RTD_A_IEC751 3.90830e-3
// #define RTD_B_IEC751 -5.77500e-7

#define RawMax 20000
#define RawMin 5000

class MyTemperature
{

private:
    /////////////定义MAX31865 CS(注意ESP32不是每个脚都能输出)//////////////////////
    const uint8_t CS_PIN[SENSER_QUANTITY] = SENSER_PIN;
    MAX31865_RTD *rtd[SENSER_QUANTITY];
    uint16_t raw[SENSER_QUANTITY];
public:
    MyTemperature();

    CalibratePoint *calibratePoint;

    uint16_t getRawResistance(uint8_t senserNumber);

    double getTemperature(uint16_t raw_t, int senserNumber);

    void recalulateAndSetCelibrate(double temperature, uint16_t *raw);

    //RawKeyPoint *rawKeyPoint;
    //void recalulateAndSetKeyPoint(double temperature, uint16_t *raw);

    //RawKeyPoint *calulateAndSetKeyPointAll();

    //double getTemperature(uint8_t senserNumber);
    //void setRawKeyPoint(RawKeyPoint *keyPoint);

    /////////////////////////////新算法///////////////////////////////////////////////////////
    //raw 求 温度
    // double temperature(uint16_t raw_0, uint16_t raw_t) const
    // {
    //     static const double a2 = 2.0 * RTD_B_IEC751;
    //     static const double b_sq = RTD_A_IEC751 * RTD_A_IEC751;
    //     double c = 1.0 - (double)raw_t / (double)raw_0;
    //     double D = b_sq - 2.0 * a2 * c;
    //     double temperature_deg_C = (-RTD_A_IEC751 + sqrt(D)) / a2;
    //     return (temperature_deg_C);
    // }

    //已知温度求Raw0
    // uint16_t calulateRaw0(double t, uint16_t raw_t) const
    // {
    //     double a = 1 + RTD_A_IEC751 * t + RTD_B_IEC751 * t * t;
    //     if (0 == a)
    //         return 0;
    //     return (uint16_t)(raw_t / a);
    // }

    //void recalulateAndSetRaw0(double temperature, uint16_t *raw);
    /////////////////////////
};

#endif